﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ATEC.ATDataCoreNET;
using ATEC.GeometryNET;
using SSDPNET;
using SSDPNET.SZEWObject;
using SSDPNET.Geom;
using ATEC.ATDataCoreNET.ATObjects;
using System.IO;

namespace SZEWComponentLibraryUtilExample
{
    public partial class ComponentLibraryTestForm : Form
    {

        public ComponentLibraryTestForm()
        {
            InitializeComponent();
        }

        public static ATProject OpenATProject(string filePath, ATProjectOpenMode openMode)
        {
            string filename = Path.GetFileName(filePath);
            ATProjectStatus fileStatus;
            ATDoc dbDoc = ATDoc.CreateFromFileName(out fileStatus, filePath, filename, -100, ATDoc.FetchMode.Write, ATDoc.FetchOptions.Default);

            if(dbDoc.DocState == ATDoc.State.DoesNotExist)
            {
                string errorMessage = "创建ATDoc失败";
                string errorMessageTitle = "创建ATDoc失败";
                ATOutputFuncs.OutputMessageCenter(ATOutputFuncs.OutputMessagePriority.Error, errorMessage,
                    errorMessageTitle, ATOutputFuncs.OutputMessageAlert.Balloon);
                return null;
            }

            ATProjectOwner dbFileOwner = ATProject.Create(dbDoc, openMode);
            ATProject dbFile = dbFileOwner.ATProject;
            StatusInt openStatus;
            ATProjectStatus status = dbFile.LoadATProject(out openStatus);
            if(ATProjectStatus.Success != status)
            {
                string errorMessage = "加载ATProject失败";
                string errorMessageTitle = "加载ATProject失败";
                ATOutputFuncs.OutputMessageCenter(ATOutputFuncs.OutputMessagePriority.Error, errorMessage,
                    errorMessageTitle, ATOutputFuncs.OutputMessageAlert.Balloon);
                return null;
            }

            dbFile.FillDictionaryScene();
            return dbFile;
        }

        // 弹窗选择构件
        private void button_SelectComp_Click(object sender, EventArgs e)
        {
            ComponentCategory comProfession = ComponentCategory.Bridge;
            ComponentLibraryInfo getCom = SZEWComponentLibraryUtil.SelectComponentByProfession(comProfession);
            //getCom.m_comInfo->m_comName;
            string mess = "未选择有效构件";
            ComponentClass compClass = getCom.GetComponentClass();
            if (compClass != ComponentClass.ERROR)
            {
                mess = string.Format("选择的构件为: , 构件名称: {0}, 构件id: {1}, 构件类型序号：{2} \n", 
                    getCom.GetComponentName(), getCom.GetComponentID(),(int)compClass);
            }
            MessageBox.Show(mess);

        }
        //获取构件数组
        private void button_GetCompList_Click(object sender, EventArgs e)
        {
            ComponentCategory comProfession = ComponentCategory.Bridge;

            List<ComponentLibraryInfo> comList = SZEWComponentLibraryUtil.GetComponentsByProfession(comProfession);
            string mess = string.Format("桥梁专业构件数目: {0}, 前10个构件为: \n", comList.Count);
            for (int i = 0; i < Math.Min(comList.Count,10); i++)
            {
                string singleInfo = string.Format("构件名称: {0}, 构件id: {1}, 构件类型序号：{2}; \n",
                    comList[i].GetComponentName(), comList[i].GetComponentID(), comList[i].GetComponentClass());
                mess += singleInfo;
            }
            MessageBox.Show(mess);

        }
        //获取构件名称
        private void button_GetCompName_Click(object sender, EventArgs e)
        {
            ComponentCategory comProfession = ComponentCategory.Bridge;
            ComponentLibraryInfo getCom = SZEWComponentLibraryUtil.SelectComponentByProfession(comProfession);

            List<string> comNames = SZEWComponentLibraryUtil.GetComponentsByClass(getCom.GetComponentClass());
            string mess = string.Format("构件类别为\"混凝土T梁\"的构件数目: {0}\n", comNames.Count);
            for (int i = 0; i < comNames.Count; i++)
            {
                string singleInfo = string.Format("构件名称: {0} ;\n", comNames[i]);
                mess += singleInfo;
            }
            MessageBox.Show(mess);
        }
        // 点放置
        private void button_PointPlace_Click(object sender, EventArgs e)
        {
            ATSceneRef scene = SSDPNET.ScenesFunctions.SceneRefGetActive();
            ComponentCategory comProfession = ComponentCategory.Bridge;
            ComponentLibraryInfo getCom = SZEWComponentLibraryUtil.SelectComponentByProfession(comProfession);

            PointInputForm ptFform = new PointInputForm();
            XPoint3d badePoint = XPoint3d.FromXYZ(10, 10, 10);
            ptFform.ShowDialog();
            if (ptFform.result == DialogResult.OK)
            {
                badePoint = XPoint3d.FromXYZ(ptFform.x, ptFform.y, ptFform.z);
            }
            XTransform3d trsfm = XTransform3d.FromTranslation(badePoint);
            bool isSucc = SZEWComponentLibraryUtil.ComponentPointPlace(getCom, trsfm, scene.GetATScene());
            if(isSucc)
                MessageBox.Show("构件点放置成功");
            else
                MessageBox.Show("构件点放置失败");
        }
        // 线放置
        private void button_LinePlace_Click(object sender, EventArgs e)
        {
            ATSceneRef scene = SSDPNET.ScenesFunctions.SceneRefGetActive();
            ComponentCategory comProfession = ComponentCategory.Bridge;
            ComponentLibraryInfo getCom = SZEWComponentLibraryUtil.SelectComponentByProfession(comProfession);

            XSegment3d line = new XSegment3d(0, 0, 0, 100000, 200000, 300000);
            BasicCurve cur = BasicCurve.CreateLine(line);
            CurveSet extrudePath = CurveSet.Create(CurveSet.BoundaryType.Open);
            extrudePath.Add(cur);

            bool isSucc = SZEWComponentLibraryUtil.ComponentLinePlace(getCom, extrudePath, scene.GetATScene());
            if (isSucc)
                MessageBox.Show("构件线放置成功");
            else
                MessageBox.Show("构件线放置失败");
        }

    }
}
